package com.quanyan.stadium.service.gameLevelScore.impl;

import com.quanyan.stadium.constants.GameConstants;
import com.quanyan.stadium.entity.TbUserFLScore;
import com.quanyan.stadium.service.gameLevelScore.GameLevelScoreApi;
import com.quanyan.stadium.service.gameLevelScore.entity.UserFLScoreEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xiaoxiao on 16/12/16.
 */
@Service
public class GameLevelScoreApiImpl implements GameLevelScoreApi{

    private final static Logger logger = LoggerFactory.getLogger(GameClubLevelScoreServiceImpl.class);


    @Autowired
    private GameUserLevelScoreServiceImpl userLevelScoreService;

    @Autowired
    private GameClubLevelScoreServiceImpl clubLevelScoreService;


    @Override
    //@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void collectLScoreIncrement(Integer ownUserId, Integer oppUserId, String score, Integer gameEventId, Integer categoryId, Integer gameScheduleId) {

        List<Integer> ownIds = new ArrayList<>();
        List<Integer> oppIds = new ArrayList<>();
        ownIds.add(ownUserId);
        oppIds.add(oppUserId);
        logger.info("开始统计对战等级分");
        //校验用户当前是否存在等级分
        if (categoryId.intValue() == GameConstants.CATEGORY_ID_BASKETBALL ||
                categoryId.intValue() == GameConstants.CATEGORY_ID_FOOTBALL) {
            clubLevelScoreService.collectLScoreIncrement(ownIds, oppIds, score, gameEventId, categoryId.byteValue(), gameScheduleId);
        }else if (categoryId.intValue() == GameConstants.CATEGORY_ID_BADMINTON ||
                categoryId.intValue() == GameConstants.CATEGORY_ID_TENNIS) {
            userLevelScoreService.collectLScoreIncrement(ownIds, oppIds, score, gameEventId, categoryId.byteValue(), gameScheduleId);
        }

    }

    @Override
    public void evalOwnFLScore(Integer uid, Double score, Integer categoryId) {
        userLevelScoreService.evalOwnFLScore(uid, score, categoryId.byteValue());
    }

    @Override
    public UserFLScoreEntity queryUserFLScoreEntity(Integer uid, Integer categoryId) {
        List<Double> rangeAndStatus = userLevelScoreService.querySelfEvalRange(uid, categoryId.byteValue());
        Double flag = rangeAndStatus.get(2);
        rangeAndStatus.remove(flag);
        UserFLScoreEntity userFLScoreEntity = new UserFLScoreEntity();
        userFLScoreEntity.setCategoryId(categoryId);
        userFLScoreEntity.setEvalScoreRange(rangeAndStatus);
        TbUserFLScore tbUserFLScore = (TbUserFLScore) userLevelScoreService.queryLatestFLScore(uid, categoryId.byteValue());
        if (null!=tbUserFLScore && tbUserFLScore.getId() > 0) {
            userFLScoreEntity.setFLScore(tbUserFLScore.getCurrentEffectScore());
            userFLScoreEntity.setIsEval(tbUserFLScore.getStatus().intValue() ^ 1);
        }else {
            userFLScoreEntity.setFLScore(0.00);
            userFLScoreEntity.setIsEval(1);
        }
        userFLScoreEntity.setIsFirstEval(flag.equals(1.00)?true:false);
        return userFLScoreEntity;
    }


}
